package com.led.datas;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.led.datatype.UserInfo;
import com.led.db.DBMgr;

public class UserAccess {
	static final String SQL_QUERY_USER_INFO_BY_NAME_PASSWORD = 
			"select id, name, password, canModify, canQuery, canOtherSee, canSeeOthers "
			+ " from userInfo "
			+ " where name = ?"
			+ " and password = ?";
	public static UserInfo getUserInfoByNamePassword(String name, String password){
		Connection con = DBMgr.getConnect();
		if(null == con){
			System.out.println("when getUserInfoByNamePassword,con is null");
			return null;
		}
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			ps = con.prepareStatement(SQL_QUERY_USER_INFO_BY_NAME_PASSWORD);
			ps.setString(1, name);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if(null == rs || rs.isAfterLast()){
				return null;
			} else {
				UserInfo userInfo = null;
				if(rs.next()){
					userInfo = new UserInfo(rs);
					System.out.println("useInfo:" + userInfo);
				} else {
					System.out.println("user or passwd is not corret, userName:" + name);
					
				}
				return userInfo;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			DBMgr.reset();
		} finally {
			if(null != rs){
				try{rs.close();} catch(Exception e){}
			}
			if(null != ps){
				try {ps.close();} catch (SQLException e) {e.printStackTrace();}
			}
		}
		return null;
	}
}
